/**
 * Example: How to create a new plugin with the decoupled system
 * This demonstrates how easy it is to add new plugins without modifying core files
 */

import * as monaco from 'monaco-editor'
import type { EditorPlugin } from '../base-types'
import type { ExamplePluginProps } from './types'

// Re-export for convenience
export type { ExamplePluginProps }

/**
 * Example Plugin Implementation
 */
export class ExamplePlugin implements EditorPlugin {
  name = 'example-plugin'
  version = '1.0.0'
  private enabled = false
  private disposables: monaco.IDisposable[] = []
  private config: ExamplePluginProps

  constructor(config: ExamplePluginProps = {}) {
    this.config = config
  }

  async initialize(): Promise<void> {
    if (this.enabled) {
      console.log(`🎯 ${this.name} already enabled, skipping initialization`)
      return
    }

    console.log(`🎯 Initializing ${this.name} v${this.version}`)
    console.log(`📝 Custom message: ${this.config.customMessage || 'Hello from Example Plugin!'}`)

    try {
      // Register a simple completion provider as example
      const completionProvider = monaco.languages.registerCompletionItemProvider(
        ['typescript'],
        {
          provideCompletionItems: (model, position) => {
            const lineContent = model.getLineContent(position.lineNumber)
            const word = model.getWordUntilPosition(position)
            
            // Trigger on custom key (default: 'example')
            if (word.word === (this.config.triggerKey || 'example')) {
              const range = new monaco.Range(
                position.lineNumber,
                word.startColumn,
                position.lineNumber,
                word.endColumn
              )
              
              return {
                suggestions: [
                  {
                    label: 'exampleFunction',
                    kind: monaco.languages.CompletionItemKind.Function,
                    insertText: 'exampleFunction()',
                    detail: 'Example Plugin Function',
                    documentation: this.config.customMessage || 'Generated by Example Plugin',
                    range: range
                  }
                ]
              }
            }
            
            return { suggestions: [] }
          }
        }
      )
      
      this.disposables.push(completionProvider)
      this.enabled = true
      console.log(`✅ ${this.name} initialized successfully`)
    } catch (error) {
      console.error(`❌ Failed to initialize ${this.name}:`, error)
      throw error
    }
  }

  dispose(): void {
    this.disposables.forEach(disposable => disposable.dispose())
    this.disposables = []
    this.enabled = false
    console.log(`🗑️ ${this.name} disposed`)
  }

  isEnabled(): boolean {
    return this.enabled
  }

  enable(): void {
    if (!this.enabled) {
      this.initialize().catch(console.error)
    }
  }

  disable(): void {
    this.dispose()
  }
}